<%@page import="java.text.DecimalFormat"%>
<%@page import="java.sql.Date"%>
<%@page import="edu.mta.pokerstats.Game"%>
<%@page import="edu.mta.pokerstats.GameRecord"%>
<%@page import="java.util.ArrayList"%>
<%@page import="edu.mta.pokerstats.Player"%>
<%@page import="edu.mta.pokerstats.Team"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	Player viewingPlayer = (Player)session.getAttribute("viewingPlayer");
	Team playerTeam = Team.getTeamById(viewingPlayer.getTeamId());
	ArrayList<Game> gamesList = new ArrayList<Game>(playerTeam.getGamesOfTeam().values());
%>
<script type="text/javascript">
	$(document).ready(function () {
		$('#PlayerDetailsTable').dataTable({
			"iDisplayLength": 50,
		    "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
			"aaSorting": [[0,"desc"]],
			"aoColumnDefs": [
					{ 'bSortable': false, 'aTargets': [7] },
					{ 'bSearchable': false, 'aTargets': [7]}
			]
		});
	});
	
	function displayGame(i_GameID){
		$.ajax({
			url : "ShowSingleGameRecords.jsp",
			type : "POST",
			data : {
				gameID : i_GameID
			},
			success : function(data, status) {
				changeDivMainContainer(data);
			}
		});
	}
</script>
<%
	if (viewingPlayer.isTeamLeader()){
%>
<script type="text/javascript" src="js/editDeleteGames.js"></script>
<%
	}
%>
<h3>Games played by Team "<%=playerTeam.getName()%>"</h3>

<div id="statistics">
	<p id="statisticsParagraph">
		Average of total pot : <span id="avgPotTarget"></span><br>
		Average number of players in a game : <span id=avgPlayersTarget></span><br>
	</p>
</div>

<table id="PlayerDetailsTable" class="display">
	<thead>
		<tr>
			<th>Date</th>
			<th>Buy-In</th>
			<th>Rebuy</th>
			<th>Remark</th>
			<th>Total Pot</th>
			<th>Players</th>
			<th>Tip</th>
			<th>Actions</th>
		</tr>
	</thead>
	<tbody>
	<%
	int numOfGames = 0;
	int sumOfPots = 0;
	int sumOfPlayers = 0;
		for (int i = 0; i < gamesList.size(); i++) {
				Game currGame = gamesList.get(i);
				int totalPot = 0;
				int totalWinnings = 0;
				int numberOfPlayers = 0;
				String remark = currGame.getRemark();
				ArrayList<GameRecord> recordsForGame = currGame.GetRecordsForGame();
				for (GameRecord currGR : recordsForGame){
					totalPot += currGame.GetInvestment(currGR.getNumberOfRebuys());
					totalWinnings += currGR.getWonInGame();
					numberOfPlayers++;
				}
				numOfGames++;
				sumOfPots+=totalPot;
				sumOfPlayers+=numberOfPlayers;
	%>
	<tr id="row<%=i + 1%>">
		<td class="noWrap" id="row<%=i + 1%>Date"><div><%=currGame.getDate()%></div></td>
		<td id="row<%=i + 1%>BuyIn"><div><%=currGame.getCostOfBuyIn()%></div></td>
		<td id="row<%=i + 1%>Rebuy"><div><%=currGame.getCostOfRebuy()%></div></td>
		<td id="row<%=i + 1%>Remark"><div><%=remark != null ? remark : ""%></div></td>
		<td id="row<%=i + 1%>TotalPot"><%=totalPot%></td>
		<td id="row<%=i + 1%>NumPlayers"><%=numberOfPlayers%></td>
		<td id="row<%=i + 1%>Tip"><%=totalPot - totalWinnings%></td>
		<td>
		<%
			if (viewingPlayer.isTeamLeader()) {
		%>
			<button id="row<%=i + 1%>EditBtn" onclick="editGame(<%=i + 1%>,<%=currGame.getId()%>)">Edit</button>
			<button id="row<%=i + 1%>SaveBtn" hidden="hidden" onclick="saveEditGame(<%=i + 1%>,<%=currGame.getId()%>)">Save</button>

			<button id="row<%=i + 1%>DeleteBtn" onclick="deleteGame(<%=i + 1%>,<%=currGame.getId()%>)">Delete</button>
			<button id="row<%=i + 1%>CancelBtn" hidden="hidden" onclick="cancelEditGame(<%=i + 1%>,<%=currGame.getId()%>)">Cancel</button>
		<%
			}
		%>
			<button onclick="displayGame(<%=currGame.getId()%>)">View Game Info</button>
		</td>
		</tr>
	<%
		} // End of For Loop
	%>
	</tbody>
</table>

<% //Calculate statistics:
	DecimalFormat twoDForm = new DecimalFormat("#.##");
	double avgPots = 0;
	double avgPlayers = 0;
	if (numOfGames > 0){
		avgPots = sumOfPots / (double)numOfGames;
		avgPlayers = sumOfPlayers / (double)numOfGames;
	}
%>

<script type="text/javascript">
	$('#avgPotTarget').html('<%= avgPots > 0 ? twoDForm.format(avgPots) : "N/A"%>');
	$('#avgPlayersTarget').html('<%=avgPlayers > 0 ? twoDForm.format(avgPlayers) : "N/A"%>');
</script>